/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pdfdataexplorer;

import java.text.ParseException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.*;
import java.io.*;
import org.hsqldb.jdbcDriver;
import java.util.*;
/**
 *
 * @author root
 */

public class DbManager
{
    public DbManager() {
    }
    /**
     * @param args the command line arguments
     */
    public void test(String filename) {
        openFile(filename);
			
		try {
			
			filename = filename.substring(0, filename.length()-4);
            System.out.println("Ciao: "+filename);

            String db_file_name_prefix = filename;

            Connection con = null;
            // Load the HSQL Database Engine JDBC driver
            // hsqldb.jar should be in the class path or made part of the current jar
            Class.forName("org.hsqldb.jdbcDriver");

            // connect to the database.   This will load the db files and start the
            // database if it is not alread running.
            // db_file_name_prefix is used to open or create files that hold the state
            // of the db.
            // It can contain directory names relative to the
            // current working directory
            con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames
                    "sa", // username
                    "");  // password

            Statement statement = con.createStatement();
            //look at " for table name
            //ResultSet rs = statement.executeQuery("SELECT * FROM tabella1");
            //ResultSet rs =
			System.out.println(		statement.execute("CREATE TABLE t (id INTEGER PRIMARY KEY)") );
					//		executeQuery();

			//print the result set
            /*while (rs.next()) {
                System.out.println("ID: " + rs.getString("id"));
            }*/

            statement.close();
            con.close();

        } catch (SQLException ex)
        {
            Logger.getLogger(DbManager.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        } catch (ClassNotFoundException ex)
        {
            Logger.getLogger(DbManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

	public void openFile(String filename) {
		try {
               //Open the zip file that holds the OO.Org Base file
               ZipFile file = new ZipFile(filename);
			   php.echo("ciao");
			   php.echo("dn: "+php.dirname(filename));

			   //Create a generic temp file. I only need to get the filename from
               //the tempfile to prefix the extracted files for OO Base
               File f = File.createTempFile("ooTempDatabase", ".tmp", new File(php.dirname(filename)));
               f.deleteOnExit();

			   ZipEntry ent = null;

               //Get file entries from the zipfile and loop through all of them
               Enumeration en = file.entries();
			    InputStream in = null;
				BufferedOutputStream out = null; //For the output from the zip class
				int len; //General length counter for loops
				List v = new ArrayList(); //Stores list of unzipped file for deletion at end of program

               while (en.hasMoreElements())
               {
                    //Get the current element
                    ent = (ZipEntry)en.nextElement();

                    //If the file is in the database directory, extract it to our
                    //temp folder using the temp filename above as a prefix
                    if (ent.getName().startsWith("database/"))
                    {
                         System.out.println("Extracting File: " + ent.getName());
                         byte[] buffer = new byte[1024];

                         //Create an input stream file the file entry
                         in = file.getInputStream(ent);

                         //Create a output stream to write out the entry to, using the
                         //temp filename created above
                         out = new BufferedOutputStream(new FileOutputStream("/tmp/" + f.getName() + "." + ent.getName().substring(9)));

                         //Add the newly created temp file to the tempfile vector for deleting
                         //later on
                         v.add("/tmp/" + f.getName() + "." + ent.getName().substring(9));

                         //Read the input file into the buffer, then write out to
                         //the output file
                         while((len = in.read(buffer)) >= 0)
                         out.write(buffer, 0, len);

                         //close both the input stream and the output stream
                         out.close();
                         in.close();
                    }
               }
               //Close the zip file since the temp files have been created
               file.close();

		
		}   catch (Exception e)
          {
				System.out.println("ciao:A");
               e.printStackTrace();
          }
	}
}